//
//  ljhCheckbox.m
//  ljhCheckbox
//
//  Created by EssIOS on 17/1/3.
//  Copyright © 2017年 ljh. All rights reserved.
#import "WHSelectBoxView.h"

@interface WHSelectBoxView ()

@property(nonatomic,strong)NSMutableArray*selectArray;
@property(nonatomic,strong)NSIndexPath *indexPath;
@property(nonatomic,strong)NSString *choice;

@end

@implementation WHSelectBoxView


- (instancetype)mybuttonwithArr:(NSArray *)arr andTitle:(NSString *)title andMessage:(NSString *)message
{
    self.baseArr= [NSMutableArray arrayWithArray:arr];
    _alertTitle=title;
    _alertMessage=message;
    return self;
}

-(void)showAlert{
    
    UIViewController *controller = [[UIViewController alloc]init];
    UITableView *alertTableView;
   
    CGRect rect;
    if (self.baseArr.count>6) {
     rect = CGRectMake(0, 0, 272, 240);
    }
    else{
     rect = CGRectMake(0, 0, 272, _baseArr.count*40);
    }
    
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:rect];
    alertTableView.delegate = self;
    alertTableView.dataSource = self;
    alertTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [alertTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [alertTableView setTag:101];
    [controller.view addSubview:alertTableView];
    [controller.view bringSubviewToFront:alertTableView];
    [controller.view setUserInteractionEnabled:YES];
    [alertTableView setUserInteractionEnabled:YES];
    [alertTableView setAllowsSelection:YES];
    alertTableView.editing=YES;
    
    self.alertTableView = alertTableView;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:_alertTitle message:_alertMessage preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController setValue:controller forKey:@"contentViewController"];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    [alertController addAction:cancelAction];
    
    UIAlertAction *corfirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
       
//        if(self.finishBlock)
//        {
//            self.finishBlock([NSString stringWithFormat:@"%ld",(long)self.indexPath.row]);
//        }
       
        [self setTitle:self.choice forState:UIControlStateNormal];
        
        if(self.finishBlock)
        {
            self.finishBlock(self.indexPath);
        }
    }];
    
    [alertController addAction:corfirmAction];
    [[self getCurrentViewController:self] presentViewController:alertController animated:YES completion:nil];
    
   // [self initSelectRow:alertTableView];
}

#pragma mark - 获取当前view的viewcontroller
- (UIViewController *)getCurrentViewController:(UIView *) currentView
{
    for (UIView* next = [currentView superview]; next; next = next.superview)
    {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
        {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
#pragma mark--tableview delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.baseArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID=@"cellID";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.contentView.backgroundColor =[UIColor whiteColor];
    
        cell.backgroundColor =[UIColor whiteColor];
        cell.backgroundView.backgroundColor =[UIColor whiteColor];
    }
   // cell.selectionStyle=UITableViewCellSelectionStyleDefault;
    //修改背景颜色
    UIView *backgroundViews = [[UIView alloc]initWithFrame:cell.frame];
    backgroundViews.backgroundColor = [UIColor whiteColor];
    [cell setSelectedBackgroundView:backgroundViews];
    
    cell.textLabel.text=self.baseArr[indexPath.row];
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(void)setDefaultSelectRow:(int)row
{
    [self.alertTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:row inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];//设置选中第一行
    [self tableView:self.alertTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:row inSection:0]];//实现点击第一行所调用的方法
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.indexPath!=nil&&self.indexPath!=indexPath)
    {
        [tableView deselectRowAtIndexPath:self.indexPath animated:YES];
    }
    self.indexPath = indexPath;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didDeselectRowAtIndexPath");
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];//设置选中第一行
    [self tableView:tableView didSelectRowAtIndexPath:indexPath];//实现点击第一行所调用的方法
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return indexPath;
}

-(NSMutableArray *)selectArray
{
    if (!_selectArray)
    {
        _selectArray =[NSMutableArray array];
    }
    return _selectArray;
}

@end


